package cn.endeavour.pet.service.impl;

import cn.endeavour.basic.exception.BusinessException;
import cn.endeavour.basic.service.impl.BaseServiceImpl;
import cn.endeavour.basic.util.LoginContextUtil;
import cn.endeavour.org.domain.Employee;
import cn.endeavour.org.mapper.EmployeeMapper;
import cn.endeavour.pet.domain.Pet;
import cn.endeavour.pet.domain.PetDetail;
import cn.endeavour.pet.mapper.PetDetailMapper;
import cn.endeavour.pet.mapper.PetMapper;
import cn.endeavour.pet.service.IPetService;
import cn.endeavour.user.domain.LoginInfo;
import cn.endeavour.user.domain.User;
import cn.endeavour.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        super.add(pet);
        // 添加宠物明细
        PetDetail detail = pet.getDetail();
        // 判断非空
        if (detail != null) {
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        super.delete(id);
        // 删除宠物明细表
        petDetailMapper.removeByPetId(id);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        // 先看是否添加了细节
        PetDetail detail = pet.getDetail();
        // 如果有进行修改
        if (detail != null) {
            detail.setPet_id(pet.getId());
            petDetailMapper.updateByPetId(detail);
        }

    }

    @Override
    public void patchDelete(List<Long> ids) {
        // 先删除宠物的id
        super.patchDelete(ids);
        // 删除宠物明细表
        petDetailMapper.patchRemoveByPetId(ids);

    }

    @Override
    @Transactional
    public void onSale(List<Long> ids) {
        // 创建一个hashmap
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        // 设置上架时间
        map.put("onSaleTime", new Date());
        // 设置状态
        map.put("state", 1);
        // 执行方法
        petMapper.onSale(map);
    }

    @Override
    @Transactional
    public void offSale(List<Long> ids) {
        // 创建一个hashmap
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        // 设置下架时间
        map.put("offSaleTime", new Date());
        // 设置状态
        map.put("state", 0);
        // 执行方法
        petMapper.offSale(map);
    }

    @Override
    @Transactional
    public void adopt(Long id,  HttpServletRequest request) {
        // 通过id查询
        Pet pet = petMapper.loadById(id);
        // 获取登录人信息
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        // 通过loginInfo来查询用户信息
        User user = userMapper.loadByLoginInfoId(loginInfo.getId());
        // 给pet的领养人设置id
        pet.setUser_id(user.getId());
        // 设置下架
        pet.setState(0);
        // 设置领养时间
        pet.setOffSaleTime(new Date());
        // 进行保存
        petMapper.updateAdopt(pet);
        // 创建订单@TODO

        // 支付@TODO
    }

    @Override
    public void isAdmin(HttpServletRequest request) {
        // 获取登录人信息
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        // 获取管理员信息
        Employee employee = employeeMapper.findByLoginInfoId(loginInfo.getId());
        // 判断是否有shop
        if (employee.getShop_id() == null) {
            throw new BusinessException("你是管理员");
        }
    }
}
